package com.example.shen.drawerlayoutdemo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.shen.drawerlayoutdemo.R;
import com.example.shen.drawerlayoutdemo.adapter.MyFragmentPagerAdapter;
import com.example.shen.drawerlayoutdemo.fragment.ContactsFragment;
import com.example.shen.drawerlayoutdemo.fragment.DiscoveryFragment;
import com.example.shen.drawerlayoutdemo.fragment.MessageFragment;

import java.util.ArrayList;


public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private RadioButton rbMessage, rbContacts, rbDiscovery;
    private ArrayList<Fragment> fragmentList;
    private MyFragmentPagerAdapter adapter;
    private DrawerLayout drawer_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化界面组件
        initView();
        //初始化ViewPager
        initViewPager();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        rbMessage = (RadioButton) findViewById(R.id.rb_message);
        rbContacts = (RadioButton) findViewById(R.id.rb_contacts);
        rbDiscovery = (RadioButton) findViewById(R.id.rb_discovery);
        rbMessage.setOnClickListener(this);
        rbContacts.setOnClickListener(this);
        rbDiscovery.setOnClickListener(this);
        drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer_layout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.i("MainActivity", "onDrawerClosed: "+"开启");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.i("MainActivity", "onDrawerClosed: "+"关闭");
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    private void initViewPager() {
        MessageFragment messageFragment = new MessageFragment();
        ContactsFragment contactsFragment = new ContactsFragment();
        DiscoveryFragment discoveryFragment = new DiscoveryFragment();

        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(messageFragment);
        fragmentList.add(contactsFragment);
        fragmentList.add(discoveryFragment);
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        //ViewPager设置适配器
        viewPager.setAdapter(adapter);
        //ViewPager显示第一个Fragment
        viewPager.setCurrentItem(0);
        //ViewPager页面切换监听
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        radioGroup.check(R.id.rb_message);
                        break;
                    case 1:
                        radioGroup.check(R.id.rb_contacts);
                        break;
                    case 2:
                        radioGroup.check(R.id.rb_discovery);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //菜单中控件点击事件
//        findViewById(R.id.rl_menu).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, getString(R.string.setting), Toast.LENGTH_SHORT).show();
//            }
//        });
        findViewById(R.id.iv).setOnClickListener(this);
        findViewById(R.id.tv).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_message:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.rb_contacts:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.rb_discovery:
                viewPager.setCurrentItem(2, false);
                break;
            case R.id.iv:
                Toast.makeText(MainActivity.this, "点了图片", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv:
                Toast.makeText(MainActivity.this, "点了文字", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
